//
//  RCActivityDetailVC.m
//  RocketProject
//
//  Created by chen jiedan on 13-10-4.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCActivityDetailVC.h"
#import "RCServerActivityListCell.h"
#import "IManager+RequestServerDetailInfo.h"
#import "RCActivityDetailInfoModel.h"
#import "RCOrderManagerVC.h"
#import "AppDelegate.h"

@interface RCActivityDetailVC ()<RCActivityAttendProtocol,IManagerDelegate>{
    RCServerActivityItemDataModel *_item;
    RCActivityDetailInfoModel *_listModel;
    int64_t _reqID;
}


@property (nonatomic, retain) RCServerActivityItemDataModel *item;
@property (nonatomic, retain) RCActivityDetailInfoModel *listModel;
@end

@implementation RCActivityDetailVC
@synthesize item = _item;
@synthesize listModel = _listModel;

- (void) dealloc
{
    [[IManager sharedInstance] cancelFetchDataRequestByRequestID:_reqID];
    self.item = nil;
    [super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (id) initWithItem:(RCServerActivityItemDataModel *)item{
    self = [super init];
    if (self) {
        self.item = item;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.titleLabelText = @"活动详情页";
    self.navigationItem.titleView = [self createTitleView];

    // 设置tableview相关参数
    if ( baseVCTableView != nil ) {
        self.view.autoresizingMask |= UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
        //        baseVCTableView.autoresizingMask |= UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
        baseVCTableView.backgroundColor = [UIColor clearColor];
        baseVCTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        baseVCTableView.frame = self.view.bounds;
        CGRect tableViewNewFrame = CGRectMake(baseVCTableView.frame.origin.x,baseVCTableView.frame.origin.y,baseVCTableView.frame.size.width,UIScreenHeight-20-self.navigationController.navigationBar.frame.size.height-4);
        baseVCTableView.frame = tableViewNewFrame;
        baseVCTableView.tableHeaderView = nil;
        baseVCTableView.tableFooterView = nil;
//        [self addPullRefresh];
    }
    [self refreshData];
    
}


- (void) refreshData
{
    [[IManager sharedInstance] cancelFetchDataRequestByRequestID:_reqID];
   _reqID = [[IManager sharedInstance] requestActivityDetailInfo:self.item.aID withDelegate:self];
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.listModel != nil) {
        return 2;
    }else{
        return 1;
    }

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else{
        return [self.listModel.listItems count];
    }
}


- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath section] == 0) {
        return 135;
    }else{
        RCActivityItemInfo *itemInfo = [self.listModel.listItems objectAtIndex:[indexPath row]];

        return [RCServerActivityItemCell heightForCell:itemInfo];
    }
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;

    if ([indexPath section] == 0) {
        cell = [[[RCServerActivityListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RCServerActivityListCell"] autorelease];
        [(RCServerActivityListCell *)cell bindData:self.item];
        [(RCServerActivityListCell *)cell setDelegate:self];
    }else{
        RCActivityItemInfo *itemInfo = [self.listModel.listItems objectAtIndex:[indexPath row]];
        cell = [[[RCServerActivityItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RCServerActivityItemCell"] autorelease];
        [(RCServerActivityItemCell *)cell bindData:itemInfo];
        
    }
    return cell;
}

- (void) didClickAtActivity:(NSString *)activityID
{
    if (activityID == nil) {
        [RCAppDelegate alertWithCustomizeString:@"暂无人员参加"];
    }else{
        RCOrderManagerVC *vc = [[RCOrderManagerVC alloc] initWithVCType:RCListType_DetailActivityManage withUniqueID:activityID];
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];
    }
    
}


-  (void) updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId
{
    
}

- (void) updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId
{
    if (dataModel.errorNO == RCErrorTypeNoError) {
        self.listModel = (RCActivityDetailInfoModel *)dataModel;
        [baseVCTableView reloadData];
    }else{

    }
}

@end
